package com.runshu.runshuwulian.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/5/15.
 */

public class MTextUtil {
    /**
     * @author yangbiyao
     * @time 2017/5/15  16:16
     * @describe 是电话
     */
    public static boolean isTel(String str) {
        String regex = "^[+]{0,1}(\\d){1,3}[ ]?([-]?((\\d)|[ ]){1,12})+$";
        return match(regex, str);
    }

    /**
     * @author yangbiyao
     * @time 2017/6/20  14:10
     * @describe 密码合法化检查
     */
    public static boolean isPassWord(String str) {
        String regex = "^([a-z0-9A-Z\\.\\@\\!\\#\\$\\%\\^\\&\\*\\(\\)]){6,20}$";
        return match(regex, str);
    }

    /**
     * @author yangbiyao
     * @time 2017/5/15  16:16
     * @describe 是手机
     */
    public static boolean isPhoneTel(String str) {
        String pattern = "^1[\\d]{10}";
        return match(pattern, str);
    }
    /**
     * @author yangbiyao
     * @time 2017/5/15  16:16
     * @describe 是手机
     */
    public static boolean isYZM(String str) {
        String pattern = "[\\d]{6}";
        return match(pattern, str);
    }
    private static boolean match(String regex, String str) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(str);
        return matcher.matches();
    }

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean checkEmail(String email) {
        boolean flag = false;
        try {
            String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
            Pattern regex = Pattern.compile(check);
            Matcher matcher = regex.matcher(email);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    //根据日期计算年龄
    public static int getAge(Date dateOfBirth) {
        int age = 0;
        Calendar born = Calendar.getInstance();
        Calendar now = Calendar.getInstance();
        if (dateOfBirth != null) {
            now.setTime(new Date());
            born.setTime(dateOfBirth);
            if (born.after(now)) {
                age = 0;
//                throw new IllegalArgumentException("error age can not be later than current date");
            } else {
                age = now.get( Calendar.YEAR) - born.get( Calendar.YEAR);
                int nowDayOfYear = now.get( Calendar.DAY_OF_YEAR);
                int bornDayOfYear = born.get( Calendar.DAY_OF_YEAR);
                System.out.println("nowDayOfYear:" + nowDayOfYear + " bornDayOfYear:" + bornDayOfYear);
                if (nowDayOfYear < bornDayOfYear) {
                    age -= 1;
                }
            }
        }
        return age;
    }




    /**
     * 检测是否有emoji表情
     *
     * @param source
     * @return
     */
    public static boolean containsEmoji(String source) {
        int len = source.length();
        for (int i = 0; i < len; i++) {
            char codePoint = source.charAt(i);
            if (!isEmojiCharacter(codePoint)) { //如果不能匹配,则该字符是Emoji表情
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是Emoji
     *
     * @param codePoint 比较的单个字符
     * @return
     */
    private static boolean isEmojiCharacter(char codePoint) {
        return (codePoint == 0x0) || (codePoint == 0x9) || (codePoint == 0xA) ||
                (codePoint == 0xD) || ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) || ((codePoint >= 0x10000)
                && (codePoint <= 0x10FFFF));
    }
}
